<?php
/**
 * 产品属性模型管理
 * @author cjli
 *
 */
class AttributeModel extends Model
{
	
	//属性类型列表
	public static function get_model_list()
	{
		$modelList = array();
		if(S('model_list')){
			$modelList = S('model_list');
		}else{
			$modeldb = D('model');
			$models = $modeldb->select();
			foreach ($models as $r){
				$modelList[$r['id']] = $r['name'];
			}
			S('model_list', $modelList);
		}
		return $modelList;
	}
	
	/**
	 * 获取所有属性
	 */
	public static function get_all_attribute_list()
	{
		if(S('all_attribute_list')){
			$attribute_list = S('all_attribute_list');
		}else{
			$attribute_db = D('attribute');
			$attribute_array = $attribute_db->select();
			if($attribute_array){
				foreach($attribute_array as $attr){
					$attribute_list[$attr['attr_id']] = $attr;
				}
			}
			S('all_attribute_list', $attribute_list);
		}
		return $attribute_list;
	}

	/**
	 * 获取属性详细信息
	 * @param int $attr_id 属性ID
	 */
	public static function get_attribute_info($attr_id)
	{
		$attribute = array();
		$attribute_list = self::get_all_attribute_list();
		$attribute = isset($attribute_list[$attr_id]) ? $attribute_list[$attr_id] : null;
		return $attribute;
	}
	
	/**
	 * 获取属性列表
	 * @param int $model_id 模型ID
	 * @param array $filter 筛选条件
	 */
	public static function get_attribute_list($model_id, $filter = array())
	{
		$where = array(
			'model_id' => $model_id,
		);
		if($filter){
			foreach ($filter as $key => $val){
				if($val){
					$where[$key] = $val;
				}
			}
		}
		
		$attrdb = D('attribute');
		$attrdb->field('attribute.*, model.name AS model_name');
//		$attrdb->join('attribute_values ON attribute_values.attr_id = attribute.attr_id');
		$attrdb->join('model ON model.id = attribute.model_id');
		$attrdb->order('attribute.sort_order DESC');
		$attrdb->group('attribute.attr_id');
		$attrdb->where($where);
		$attributeList = $attrdb->select();
//		echo $attrdb->getLastSql();exit;
		return $attributeList;
	}
	
	/**
	 * 属性值列表
	 * @param int $attr_id 属性ID
	 */
	public static function get_attribute_values_list($attr_id)
	{
		static $list;
		if(!(isset($list[$attr_id]) && $list[$attr_id]))
		{
			$attr_values_list = array();
			$attr_val_db = D('attribute_values');
			$attr_val_db->where('attr_id='.$attr_id);
			$result = $attr_val_db->order('sort_order DESC')->select();
			if($result){
				foreach ($result as $attrv){
					$attr_values_list[$attrv['id']] = $attrv;
				}
			}
			$list[$attr_id] = $attr_values_list ? $attr_values_list : null;
		}
		return $list[$attr_id];
	}
    /**
     * 属性值信息
     * @param int $id    属性值id     $language_id  语言
     */
    public static function get_attribute_values_info($id,$language_id=6)
    {

        if($id){
            $attr_val_db = D('attribute_values');
            $attr_val_db->field("attribute_values.*,attribute_values_items.attr_value");
            $attr_val_db->join('attribute_values_items ON attribute_values_items.att_value_id = attribute_values.id');
            $attr_val_db->where('id='.$id." and language_id=".$language_id);
            $result = $attr_val_db->find();
        }//echo $attr_val_db->getLastSql();exit;
        return $result;
    }
	/**
	 * 获取购物车商品属性
	 * @param array $attr 关联属性ID
	 * @return array
	 */
	public static function get_product_attribute_List($attr)
	{
		$product_attribute_db = D('product_attribute pa');
		$product_attribute_db->field('attribute.attr_id,attribute.attr_name,pa.attr_value,pa.attr_price,av.attr_value AS attr_name_value,av.attr_img');
		$product_attribute_db->join('attribute ON attribute.attr_id = pa.attr_id');
		$product_attribute_db->join('attribute_values AS av ON av.id = pa.attr_value_id');
		$product_attribute_db->where('pa.product_attr_id IN ('. implode(',',$attr) .')');
		$product_attribute_db->order('attribute.sort_order,pa.product_attr_id');
		$attributeList = $product_attribute_db->select();
		if($attributeList){
			foreach ($attributeList as $key => $attr){
				if(empty($attr['attr_value'])){
					$attributeList[$key]['attr_value'] = $attr['attr_name_value'];
				}
			}
		}
//		echo $product_attribute_db->getLastSql();exit;
		return $attributeList;
	}
	
	/**
   * 返回商品属性值html显示类型
   * @param array $attribute 属性数组信息
   * @param int $product_id 作品ID
   * @return string
   */
	public static function get_attribute_values_html($model_id, $product_id, $attr_filter = array(),$designer_id=0)
	{
		$product_attribute_db = D('product_attribute');
		$html = '<div>';
		$spec = 0;
		$attributeList = self::get_attribute_list($model_id, $attr_filter);
		if($attributeList)
		{
			foreach ($attributeList as $attribute)
			{
				$attribute_id = $attribute['attr_id'];
				 
				 //属性值列表
				$attributeValuesList = self::get_attribute_values_list($attribute_id);

				//Input 名称
				$fieldName = 'field_' . $attribute_id;
				//默认信息
				$defaultValue = array();
				$is_default = 0;
				
				if($designer_id){
					$product_attribute_db->where(array('attr_id'=>$attribute_id, 'designer_id'=>$designer_id));
			 		$productAttributeList = $product_attribute_db->select();
					//echo $product_attribute_db->getLastSql();
			 		//多值
			 		if($attribute['attr_type'] == 2){
			 			foreach ($productAttributeList as $key => $val){
			 				if($val['is_default']){
			 					$is_default = $val['is_default'];
			 				}
			 				if($attribute['attr_input_type']==0){
			 					$defaultValue[] = $val['attr_value'];
			 				}else{
			 					$defaultValue[$val['attr_value_id']] = $val;
			 				}
			 			}
			 		}else{
			 			$defaultValue = $productAttributeList ? reset($productAttributeList) : null;
			 		}
				}else{
					if($product_id){
	//			 		$product_attribute_db->field('attr_value_id,attr_value');
						$product_attribute_db->where(array('attr_id'=>$attribute_id, 'product_id'=>$product_id));
						$productAttributeList = $product_attribute_db->select();
	//			 		echo $product_attribute_db->getLastSql();exit;
						//多值
						if($attribute['attr_type'] == 2){
							foreach ($productAttributeList as $key => $val){
								if($val['is_default']){
									$is_default = $val['is_default'];
								}
								if($attribute['attr_input_type']==0){
									$defaultValue[] = $val['attr_value'];
								}else{
									$defaultValue[$val['attr_value_id']] = $val;
								}
							}
						}else{
							$defaultValue = $productAttributeList ? reset($productAttributeList) : null;
						}
					}
				}
				

		//手工录入
				if($attribute['attr_input_type']==0)
				{
					//单选
		      	if($attribute['attr_type'] == 1)
		      	{
		      		$html .= "<tr><td class='label' width=\"150\">".$attribute['attr_name']."</td><td>";
		        	$html .= '<input type="text" class="input-text" name="' . $fieldName . '" value="' . isset($defultValue['attr_value']) ? $defultValue['attr_value'] : null . '"></td></tr>';
		      	}
		      	else	//多选
		      	{
		      		if($defaultValue){
			      		foreach ($defaultValue as $default_val){
				         	$html .= "<tr><td class='label' width=\"150\">";
						 			$html .= ($spec != $attribute_id) ? "<a href='javascript:;' onclick='addSpec(this)'>[+]</a>" : "<a href='javascript:;' onclick='removeSpec(this)'>[-]</a>";
						 			$html .= $attribute['attr_name']."</td><td>";
						 			$html .= '<input type="text"  class="input-text" name="' . $fieldName . '[]" value="' . (isset($default_val) ? $default_val : null) . '"></td></tr>';
						 			$spec = $attribute_id;
			      		}
		      		}else{
		      			$html .= "<tr><td class='label' width=\"150\">";
						 		$html .= ($spec != $attribute_id) ? "<a href='javascript:;' onclick='addSpec(this)'>[+]</a>" : "<a href='javascript:;' onclick='removeSpec(this)'>[-]</a>";
						 		$html .= $attribute['attr_name']."</td><td>";
						 		$html .= '<input type="text"  class="input-text" name="' . $fieldName . '[]" value=""></td></tr>';
						 		$spec = $attribute_id;
		      		}
		      	}
				}
				//列表选择
				elseif($attribute['attr_input_type']==1 && $attributeValuesList)
				{
					$html .= "<div class=\"cliao2-head\">".$attribute['attr_name']."</div><div class=\"cliao3\"><ul>";
		      	foreach ($attributeValuesList as $fvalue)
		      	{
		      		$attribute_values_id = isset($defaultValue[$fvalue['id']]) ? $fvalue['id'] : null;
		      		
		      		$html .='<li class="attr_value'.($is_default==$fvalue['id'] ? ' is_default' : '').'">';
		      		//显示图片
		      		if($fvalue['attr_img']){
		      			$html .= '<img src="'.$fvalue['attr_img'].'" width="67" height="67"/>';
		      		}
		      		$html .='<div>';
		      		//单选
				      if($attribute['attr_type'] == 1)
				      {
				      	$html .= '<input name="' . $fieldName . '[]" type="radio" value="' . $fvalue['id'] . '"' . ($fvalue['id'] == $attribute_values_id ? ' checked="checked"' : '') . '>';
				      }else{//多选
				      	$html .= '<input name="' . $fieldName . '[]" type="checkbox" value="' . $fvalue['id'] . '"' . ($fvalue['id'] == $attribute_values_id ? ' checked="checked"' : '') . '>';
				      }
				      $html .= '<span>'. $fvalue['attr_value'] . '</span> </div></li>';
		       }
		       $html .='<input type="hidden" name="' . $fieldName . '[default]" value="'.$is_default.'">';
		       $html .= '</ul></div>';
				}
			}
    }
    $html .= '</div><div class="clear"></div>';
    return $html;
	}
	
	/**
	 * 获取尺码对照列表
	 * @return array
	 */
	public static function get_user_size_List()
	{
		$product_user_size_db = D('product_user_size');
		$userSizeList = $product_user_size_db->cache(true,C('DB_CACHE_TIME'))->select();
		return $userSizeList;
	}
	
	/**
	 * 获取身形列表详情
	 * @return array
	 */
	public static function get_user_measure_info($mid)
	{
		$user_measure_db = D('user_measure');
		$measureInfo = $user_measure_db->find($mid);
		return $measureInfo;
	}
	
	/**
	 * 获取身形列表
	 * @return array
	 */
	public static function get_user_measure_list()
	{
		global $user;
		if (isset($user['id']) && $user['id']) {
			$where['user_id'] = $user['id'];
		}else{
			$where['user_id'] = 0;
			$where['sid'] = $user['sid'];
		}
		$user_measure_db = D('user_measure');
		$user_measure_db->where($where);
		$measureList = $user_measure_db->select();
		//echo $user_measure_db->getLastSql();
		return $measureList;
	}

	/**
	 * 获取一个最近更新过身形的身型id
	 * @return $mid
	 */
	public static function get_user_measure_mid()
	{
		global $user;
		if (isset($user['id']) && $user['id']) {
			$where['user_id'] = $user['id'];
		}else{
			$where['user_id'] = 0;
			$where['sid'] = $user['sid'];
		}
		$user_measure_db = D('user_measure');
		$user_measure_db->where($where);
		$mid = $user_measure_db->order('update_time desc')->getField('id');
		return $mid;
	}

	/**
	 * 合并身型数据
	 * @param string $sid 会话ID
	 * @return boolean
	 */
    public static function mergeMeasure($sid = null)
    {
	  	global $user;
	  	if (!session('user_id')) {
	  		return false;
	  	}
	  	if (!isset($sid)) {
	      $sid = $user['sid'];
	    }
		$user_measure_db = D('user_measure');
		$user_measure_db->where($where);
	    $affected = $user_measure_db->where(array('sid' => $sid,'user_id' => 0))->save( array('user_id' => session('user_id'),'sid' => 0) );
	    return (boolean)$affected;
    }
	
	/**
	 * 获取选项提示
	 * @param int $aid 选项id
	 * @return array
	 */
    public function attribute_info($aid)
	{
        global $language_id;
		$aid = intval($aid);
		if($aid){
			$attribute_items_db = D('attribute_items');
			$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $aid,'language_id' => $language_id))->find();

			// $res = array(
			// 	'name' => $attribute_items_info['name'],
			// 	'content' => $attribute_items_info['attr_content'],
			// );
			return $attribute_items_info['attr_content'];
		}else{
			return '';
		}
		
		
	}
}